package utils;

/**
 * @Author Su
 * @Date 2025/4/30 10:06
 * @Version 1.0 （版本号）
 */
// VerificationCodeUtil.java
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Random;

public class VerificationCodeUtil {
    private static final String NUMBERS = "0123456789";
    private static final int CODE_LENGTH = 6;
    private static final int EXPIRE_MINUTES = 1; // 验证码5分钟后过期

    // 生成6位数字验证码
    public static String generateCode() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(CODE_LENGTH);
        for (int i = 0; i < CODE_LENGTH; i++) {
            sb.append(NUMBERS.charAt(random.nextInt(NUMBERS.length())));
        }
        return sb.toString();
    }

    // 计算过期时间
    public static Date getExpireTime() {
        Instant now = Instant.now(); // UTC 时间 instant java。time里面
        Instant expireInstant = now.plusSeconds(EXPIRE_MINUTES * 60);

        // 转换为 UTC+8（北京时间）
        ZonedDateTime expireTime = expireInstant.atZone(ZoneId.of("Asia/Shanghai"));
        return Date.from(expireTime.toInstant());
    }
}